package com.yanqu.road.server.cmd.activity.stronghold.eliminate;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.stronghold.StrongholdEliminateDefendPatrons;
import com.yanqu.road.entity.union.UnionMember;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdWarMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.UnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Cmd(code = GameProtocol.S_STRONGHOLD_UN_DISPATCH_MEMBER_LIST, desc = "")
public class UnDispatchMemberListCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = StrongholdActivityMgr.getOpenActivityInfo();
        if (activityInfo != null) {
            String unionUid = player.getUserInfo().getUnionUid();
            List<StrongholdEliminateDefendPatrons> defendList = StrongholdWarMgr.getEliminateDefendList(activityInfo.getActivityId(), unionUid);
            Map<Long, CrossUnionMember> unionMemberMap = CrossUnionMgr.getUnionMemberMap(unionUid);
            List<UnionMember> unDispatchList = new ArrayList<>();
            for (UnionMember member : unionMemberMap.values()) {
                boolean dispatch = false;
                for (StrongholdEliminateDefendPatrons patrons : defendList) {
                    if (member.getUserId() == patrons.getUserId()) {
                        dispatch = true;
                        break;
                    }
                }
                if (!dispatch) {
                    unDispatchList.add(member);
                }
            }
            StrongholdWarProto.GetUnDispatchMemberListRespMsg.Builder resp = StrongholdWarProto.GetUnDispatchMemberListRespMsg.newBuilder();
            for (UnionMember member : unDispatchList) {
                StrongholdWarProto.UnDispatchMemberDataMsg.Builder msg = StrongholdWarProto.UnDispatchMemberDataMsg.newBuilder();
                msg.setUserId(member.getUserId());
                msg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(member.getUserBaseInfo()));
                msg.setTodayContribution(member.getTodayContribution());
                msg.setTotalContribution(member.getTotalContribution());
                resp.addMember(msg);
            }
            player.sendPacket(ClientProtocol.U_STRONGHOLD_UN_DISPATCH_MEMBER_LIST, resp);
        }
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
